using MCDomain.DataAccess;

namespace McReports.ViewModel
{
    public abstract class WordReportViewModel : BaseReportViewModel
    {
        protected WordReportViewModel(IContractRepository repository)
            : base(repository)
        {
            
        }

        protected override void OnDispose()
        {
            _word = null;
            base.OnDispose();
        }


        private static Microsoft.Office.Interop.Word.Application _word;

        public static Microsoft.Office.Interop.Word.Application Word
        {
            get { return _word ?? (_word = new Microsoft.Office.Interop.Word.Application()); }
        }

        private Microsoft.Office.Interop.Word.Document _doc;
        public Microsoft.Office.Interop.Word.Document CurrentDocument
        {
            get { return _doc; }
        }

        public override void SetReport()
        {
            _doc = Word.Documents.Add(ReportTemplate);
            BuildReport();
            Word.ActiveWindow.Activate();
            Word.Visible = true;
            _word = null;
        }

        protected void ReplaceText(string text, string replacetext)
        {
            Word.Selection.Find.ClearFormatting();
            Word.Selection.Find.Replacement.ClearFormatting();
            Word.Selection.Find.Text = text;
            Word.Selection.Find.MatchCase = true;
            Word.Selection.Find.MatchWholeWord = true;
            Word.Selection.Find.Forward = true;
            Word.Selection.Find.Replacement.Text = replacetext;
            Word.Selection.Find.Execute(text, true, true, false, false, false, true, 
                                        Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue, false, replacetext, 
                                        Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll, false, false, false, false); 
        }
    }
}